<?php
/**
 * MapsController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
require_once 'application/models/Wanna.php';
require_once 'application/models/Friends.php';
require_once 'application/models/User.php';
require_once 'application/models/Glocation.php';

class MapsController extends Zend_Controller_Action
{
    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {
        $auth = Zend_Auth::getInstance();
        if (! $auth->hasIdentity()) {
            $this->_redirect('/user/loginform');
        }
        
        $user = $auth->getIdentity();
        $real_name = $user->first_name;
        $username = $user->user_name;
        

    	$wanna = new Wanna();
		$mywannalist = $wanna->getMyWanna($user->id);
		$myfriendswannalist = $wanna->getFriendsWanna($user);
        $this->view->mywanna=$mywannalist;
        $this->view->friendswanna=$myfriendswannalist;
        
    }
    /**
     * The default action - show the home page
     */
    public function friendsAction ()
    {
    	$auth = Zend_Auth::getInstance();
        if (! $auth->hasIdentity()) {
            $this->_redirect('/user/loginform');
        }
        
        $user = $auth->getIdentity();
        $real_name = $user->first_name;
        $user_name = $user->user_name;
        
		$friend = new Friends();
		$friendrowset=$friend->getFriendsWanna($user_name);
		
		$makerstr="";
		$eatCount = 0;
		$pickupCount = 0;
		$shoppingCount = 0;
		$i = 0;
		foreach($friendrowset as $data){
			
			$wannaType=$data['wannaType'];
			$wannaDescription=$data['WannaDescription'];
			$latitude=$data['latitude'];
			$longtitude=$data['longtitude'];
		    $makerstr=$makerstr."    var makerlng".$i." = new google.maps.LatLng(".$latitude.", ".$longtitude.");\r\n";
		    $makerstr=$makerstr."    var marker".$i." = new google.maps.Marker({position: makerlng".$i.",map:null,title:\"".$i."\"});\r\n";
		    if ($wannaType==1){
		    	$makerstr=$makerstr."    eatMarkersArray.push(marker".$i.");\r\n";
		    	$eatCount++;
		    }else if($wannaType==2){
		    	$makerstr=$makerstr."    pickupMarkersArray.push(marker".$i.");\r\n";
		    	$pickupCount++;
			}else if($wannaType==3){
		    	$makerstr=$makerstr."    shoppingMarkersArray.push(marker".$i.");\r\n";
		    	$shoppingCount++;
			}
			$makerstr=$makerstr."    contentString = '<div id=\"content\">'+ \r\n";
		    $makerstr=$makerstr."                    '<div id=\"siteNotice\"> </div>'+\r\n";
		    
		    $makerstr=$makerstr."                    '<h1 id=\"firstHeading\" class=\"firstHeading\">Wanna</h1>'+\r\n";
		    $makerstr=$makerstr."                    '<div id=\"bodyContent\">'+ \r\n";
		    $makerstr=$makerstr."                    '<p><b>Wanna</b>, also referred to ' + \r\n";
		    $makerstr=$makerstr."                    '<b> ".$wannaDescription." </b>'+ \r\n";
		    $makerstr=$makerstr."                    '<a href=\"http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194\">a</a>'+ \r\n";
		    $makerstr=$makerstr."                    '</p> </div> </div>'; \r\n";
		
			$makerstr=$makerstr."    var infowindow".$i." = new google.maps.InfoWindow({ \r\n";
		    $makerstr=$makerstr."			content: contentString\r\n";
			$makerstr=$makerstr."		}); \r\n";
			
			$makerstr=$makerstr."    google.maps.event.addListener(marker".$i.", 'click', function() { \r\n";
		  	$makerstr=$makerstr."		infowindow".$i.".open(map,marker".$i.");\r\n";
			$makerstr=$makerstr."		});\r\n \r\n";
			$i++;
		}
		$this->view->markerstr  = $makerstr;

    }
	/**
     * The default action - show the home page
     */
    public function routeAction ()
    {
    	
		$mapuserid=$this->_request->getPost('mapuserid');
		$phonenumber=$this->_request->getPost('phonenumber');
		$auth = Zend_Auth::getInstance();
        if (! $auth->hasIdentity()) {
            $this->_redirect('/user/loginform');
        }
        
        $user = $auth->getIdentity();
        $real_name = $user->first_name;
        $user_name = $user->user_name;
		// Handle must consist solely of alphanumeric characters
		$validHandle = new Zend_Validate_Alnum();
		if ($mapuserid!="")
		{
			if (! $validHandle->isValid($mapuserid)) {
				(array)$this->view->errorsDes[] = "Handle must consist of letters and numbers.";
			} // end valid handle
		}
		$location = new Glocation();
		
		if (($mapuserid=="")&&($phonenumber==""))
		{
			$locationrowset = $location->getLocationByUsername($user_name);
		}else {
			if (($mapuserid!="")&&($phonenumber!="")){
				$query="select * from glocation where userid='$mapuserid' and phonenumber = '".$phonenumber."' order by createTime;";
			}else if ($mapuserid!=""){
				$locationrowset = $location->getLocationByUserID($mapuserid);
			}else if ($phonenumber!=""){
				$query="select * from glocation where phonenumber='$phonenumber' order by createTime;";
			}
		}
		//echo $query;
		
		$makerstr="";
		$linestr="";
		$arrowstr="";
		$preLng = "";
		$preLat = "";
		$longtitude = "";
		$latitude = "";
		$arrowstrArray=array();
		$i=0;
		$rows_sum = count($locationrowset);
		foreach ($locationrowset as $data)
		{
			$arrowstr="";

			$preLng = $longtitude;
			$preLat = $latitude;
			$latitude=$data['latitude'];
			$longtitude=$data['longtitude'];
			$createTime=$data['createTime'];
		    $makerstr=$makerstr."var makerlng".$i." = new google.maps.LatLng(".$latitude.", ".$longtitude.");\r\n";
		    $makerstr=$makerstr."var marker".$i." = new google.maps.Marker({position: makerlng".$i.",map:map,title:\"".$createTime."\"});\r\n";
		    if ($i!=$rows_sum-1){
		    	$linestr=$linestr." makerlng".$i.",\r\n";
		    }else{
		    	$linestr=$linestr." makerlng".$i."\r\n";
		    }
		    $diff = 0;
		    $diff1 = 0;
		    $diff2 = 0;
		
		    if ($i!=0){
		    	$diff1 = floatval($preLat)-floatval($latitude);
		    	if ($diff1==0)
		    	{
		    		$diff1 = 0.001;
		    	}
		    	
		    	
		    	$diff2 = floatval($preLng)-floatval($longtitude);
		    	if ($diff2==0)
		    	{
		    		$diff2 = 0.001;
		    	}
		    	if (abs($diff1)>abs($diff2))
		    	{
		    		$diff=abs($diff2);
		    	}else {
		    		$diff=abs($diff1);
		    	}
		    	
		    	$midlat = floatval($latitude)+ $diff*(($diff1)/abs($diff1))/18;
		    	$midlng = floatval($longtitude)+ $diff*(($diff2)/abs($diff2))/18;
		    }
		    
		    if ($i!=0){
		    	$arrowstr=$arrowstr."   new google.maps.LatLng(".$latitude.", ".$longtitude."),\r\n";
		    	$arrowstr=$arrowstr."      new google.maps.LatLng(".$latitude.", ".$midlng."),\r\n";
		    	$arrowstr=$arrowstr."      new google.maps.LatLng(".$midlat.", ".$longtitude."),\r\n";
		    	$arrowstr=$arrowstr."      new google.maps.LatLng(".$latitude.", ".$longtitude.")\r\n";
		    	array_push($arrowstrArray,$arrowstr);
		    }
		    //}else if(($i!=0)&&($i==$rows_sum-2)){
		    //	$arrowstr=$arrowstr." new google.maps.LatLng(".$latitude.", ".$longtitude."),\r\n";
		    //	$arrowstr=$arrowstr." new google.maps.LatLng(".$latitude.", ".$midlng."),\r\n";
		    //	$arrowstr=$arrowstr." new google.maps.LatLng(".$midlat.", ".$longtitude."),\r\n";
		    //	$arrowstr=$arrowstr." new google.maps.LatLng(".$latitude.", ".$longtitude.")\r\n\r\n";
		    //	break;
		    //}
		    $i++;
		    
		}
    	$num=0;					
		while (list($key,$value) = each($arrowstrArray)) {
				
			$triaglestr= "\r\n   var triangleCoords".$num."=[\r\n   ".$value."   ];\r\n";			    
		    // Construct the polygon
		    // Note that we don't specify an array or arrays, but instead just
		    // a simple array of LatLngs in the paths property
			$triaglestr= $triaglestr . "    bermudaTriangle".$num." = new google.maps.Polygon({\r\n";
			$triaglestr= $triaglestr . "          paths: triangleCoords".$num.",\r\n";
			$triaglestr= $triaglestr . "          strokeColor: \"#FF0000\",\r\n";
			$triaglestr= $triaglestr . "          strokeOpacity: 0.8,\r\n";
			$triaglestr= $triaglestr . "          strokeWeight: 2,\r\n";
			$triaglestr= $triaglestr . "          fillColor: \"#FF0000\",\r\n";
			$triaglestr= $triaglestr . "          fillOpacity: 0.35\r\n";
			$triaglestr= $triaglestr . "          });\r\n";
			$triaglestr= $triaglestr . "    bermudaTriangle".$num.".setMap(map);	\r\n";
			$num=$num+1;
		}
		$this->view->triaglestr=$triaglestr;
		$this->view->makerstr=$makerstr;
		$this->view->linestr=$linestr;
    }
    
}
